﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System;
public class AssetBundleManager
{
    //已经加载AB包列表
    private static Dictionary<string, AssetBundle> allAssets = new Dictionary<string, AssetBundle>();
    //加载多个资源
    public static void LoadMoreAssetBundle(string[] assetNames)
    {
#if !UNITY_EDITOR || AssetBundle
        for (int i = 0; i < assetNames.Length; i++)
        {
            LoadAssetBundle(assetNames[i]);
        }
#endif
    }
    //卸载多个AB包
    public static void UnLoadMoreAssetBundle(string[] assetNames)
    {
#if !UNITY_EDITOR || AssetBundle
        for (int i = 0; i < assetNames.Length; i++)
        {
            UnLoadAssetBundle(assetNames[i]);
        }
#endif
    }
    //加载一个AB包
    public static void LoadAssetBundle(string assetName)
    {
#if !UNITY_EDITOR || AssetBundle
        if (allAssets.ContainsKey(assetName))
        {
            Log.LogError("------你试图重复加载资源------>>" + assetName+"<<");
            return;
        }
        AssetBundle assetBundle = null;
        if (File.Exists(Application.persistentDataPath + "/" + assetName + ".zxy"))
        {
            assetBundle = AssetBundle.LoadFromFile(Application.persistentDataPath + "/" + assetName + ".zxy");
        }
        else
        {
            assetBundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + assetName + ".zxy");
        }
        if (assetBundle == null)
        {
            Log.LogError("------加载资源包失败------>>" + assetName + "<<");
            return;
        }
        allAssets.Add(assetName, assetBundle);
        Log.LogBlue("------成功加载资源------>>" + assetName + "<<");
#endif
    }
    //卸载一个AB包
    public static void UnLoadAssetBundle(string assetName)
    {
#if !UNITY_EDITOR || AssetBundle
        if (!allAssets.ContainsKey(assetName))
        {
            Log.LogError("------你试图卸载一个不存在的资源------>>" + assetName + "<<");
            return;
        }
        AssetBundle asset;
        allAssets.TryGetValue(assetName, out asset);
        allAssets.Remove(assetName);
        asset.Unload(true);
        Log.LogBlue("------成功卸载资源------>>" + assetName + "<<");
#endif
    }
    //加载一个资源
    public static T LoadAsset<T>(string assetName, string path) where T:UnityEngine.Object
    {
#if !UNITY_EDITOR || AssetBundle
        if (!allAssets.ContainsKey(assetName))
        {
            Log.LogError("------资源AB包并未加载------>>" + assetName + "<<");
            return default(T);
        }
        path = path.Replace('.', '/');
        AssetBundle asset;
        allAssets.TryGetValue(assetName, out asset);
        return asset.LoadAsset<T>(path);
#else
        path = path.Replace('.', '/');
        string dirName = assetName.Split('_')[0];
        if (!File.Exists(Application.dataPath + "/AllRes/module/" +dirName+"/"+ assetName + "/" + path + GetNameByType<T>()))
        {
            Log.LogError("------资源路径不正确，请检查------>>" + dirName + "/" + assetName+"/"+ path +GetNameByType<T>()+ "<<");
            return null;
        }
        return AssetDatabase.LoadAssetAtPath<T>("Assets/AllRes/module/" + dirName + "/" + assetName+"/"+ path + GetNameByType<T>());
#endif
    }
    private static string GetNameByType<T>()
    {
        Type type = typeof(T);
        string typeName= type.Name;
        switch (typeName)
        {
            case "GameObject":
                return ".prefab";
            case "Sprite":
                return ".png";
            case "TextAsset":
                return ".txt";
        }
        Log.LogError("------类型" + typeName + "并未设置文件后缀，请设置------");
        return null;
    }
    public static void DestoryAssets()
    {
        allAssets = null;
        AssetBundle.UnloadAllAssetBundles(true);
        Log.LogError("------AssetBundle资源已消除------");
    }
}
